#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
// #include <windows.h>
#include <stdexcept>
#include <limits>

using namespace std;

class File
{
public:
    File(const char *fileName)
    {
        // SetConsoleOutputCP(65001);
        fin.open(fileName, ios::in | ios::binary); // 没有新建功能，文件不存在打开就失败
        if (!fin.is_open())
        {
            cout << "文件打开失败" << endl;
            exit(1);
        }

        readFile();
    }

    void readFile()
    {
        string line; // 逐行读取文件内容
        while (getline(fin, line))
        {
            istringstream iss(line);
            string key, value;

            // 读取键（直到 =>）
            if (getline(iss, key, '='))
            {
                // 跳过 '>' 字符
                iss.ignore(1);
                // 跳过空格和引号
                iss.ignore(1, '"');
                // 读取值直到下一个引号
                if (getline(iss, value, '"'))
                {
                    pinyinMap[key] = value;
                }
            }
        }
    }

    ~File()
    {
        fin.close();
    }

    string operator()(const string &pinyin)
    {
        return pinyinMap.at(pinyin);
    }

private:
    ifstream fin;
    map<string, string> pinyinMap;
};

void show(string &value)
{
    int pagesize = 3 * 5; // 每页显示5个汉字
    int currentPage = 1;
    int totalPages = 0;

    // 计算总页数
    totalPages = (value.size() + pagesize - 1) / pagesize;
    cout << value.size() << endl;
    cout << totalPages << endl;

    while (true)
    {
        int start = (currentPage - 1) * pagesize;
        int end = start + pagesize;
        if (end > value.size())
        {
            end = value.size();
        }

        system("clear");

        cout << "      第 " << currentPage << "/" << totalPages << " 页" << endl;
        cout << "-------------------------" << endl;
        for (int i = start; i < end; i += 3)
        {
            if (i + 3 <= value.size())
            {
                cout << value.substr(i, 3) << " ";
            }
        }
        cout << endl;
        cout << "-------------------------" << endl;

        // 分页控制
        if (totalPages <= 1)
        {
            cout << "只有一页，按回车继续..." << endl;
            cin.get();
            system("clear");
            break;
        }

        cout << "操作: a-下一页, d-上一页, q-退出分页: ";
        char cmd;
        cin >> cmd;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        if (cmd == 'q' || cmd == 'Q')
        {
            system("clear");
            break; // 退出分页循环
        }
        else if (cmd == 'd' || cmd == 'D') // d = down/next
        {
            if (currentPage < totalPages)
            {
                currentPage++;
            }
            else
            {
                cout << "已经是最后一页了!" << endl;
            }
        }
        else if (cmd == 'a' || cmd == 'A') // a = above/previous
        {
            if (currentPage > 1)
            {
                currentPage--;
            }
            else
            {
                cout << "已经是第一页了!" << endl;
            }
        }
        else
        {
            cout << "无效命令!" << endl;
        }
    }
}

int main()
{
    system("clear");
    File file("./file");
    while (1)
    {
        cout << "      酷哥输入法" << endl;
        cout << "-------------------------" << endl;
        cout << "请输入拼音: ";
        string pinyin;
        cin >> pinyin;

        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        if (pinyin == "exit")
        {
            break;
        }
        try
        {

            string value = file(pinyin);
            show(value);
            // cout << value << endl;
        }
        catch (const std::out_of_range &e)
        {
            cout << "没有找到" << pinyin << "对应的拼音!" << endl;
        }
    }
    return 0;
}
